package com.huixuebao.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huixuebao.api.dto.MechanismDTO;
import com.huixuebao.api.entity.MechanismEntity;
import com.huixuebao.api.entity.MerchantBasicsEntity;
import com.huixuebao.api.mapper.MechanismMapper;
import com.huixuebao.api.service.MechanismService;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @version 1.0
 * @date 2020-11-16 21:55
 */
@Service
public class MechanismServiceImpl extends ServiceImpl<MechanismMapper, MechanismEntity> implements MechanismService {

    @Autowired
    MechanismMapper mechanismMapper;
    /**
     * 查询没有xy坐标的机构
     *
     * @return
     */
    @Override
    public ApiResult<List<MechanismDTO>> getMerchantWithoutAxis() {

        List<MechanismEntity> list = this.list();
        List<MechanismDTO> result = new ArrayList<>();
        list.stream().forEach(entity -> {
            if (StrUtil.isBlank(entity.getAxisX()) || StrUtil.isBlank(entity.getAxisY())) {
                MechanismDTO mechanismDTO = new MechanismDTO();
                BeanUtil.copyProperties(entity, mechanismDTO);
                result.add(mechanismDTO);
            }
        });
        ApiResult apiResult = new ApiResult();
        apiResult.setData(result);
        apiResult.setSuccess(true);
        return apiResult;
    }

    /**
     * 设置机构xy坐标
     *
     * @param mechanismDTO
     * @return
     */
    @Override
    public ApiResult setMerchantAxis(MechanismDTO mechanismDTO) {
        ApiResult apiResult = new ApiResult();
        try{
            String axisY = mechanismDTO.getAxisY();
            String axisX = mechanismDTO.getAxisX();
            String province = mechanismDTO.getProvince();
            String city = mechanismDTO.getCity();
            String area = mechanismDTO.getArea();
            this.update(new MechanismEntity()
                    .setAxisX(axisX)
                    .setAxisY(axisY)
                    .setProvince(province)
                    .setCity(city)
                    .setArea(area)
                            .setCityId(province + "," + city + "," + area),
                    new LambdaQueryWrapper<MechanismEntity>()
                            .eq(MechanismEntity::getUserNubmer, mechanismDTO.getUserNubmer()));
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("添加坐标信息成功");
        }catch (Exception e){
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("添加坐标信息失败");
        }
        return apiResult;
    }

    @Override
    public ApiResult setMerchanismFromS(List<MerchantBasicsEntity> mbeList) {
        ApiResult apiResult = new ApiResult();
        if(mbeList == null && mbeList.isEmpty()){
            apiResult.setCode(ResultCodeEnum.ERROR_SSNULL.getCode());
            apiResult.setCode(ResultCodeEnum.ERROR_SSNULL.getMessage());
            return  apiResult;
        }
        for (int x=0;x<mbeList.size();x++) {
            MechanismEntity mechanismEntity = new MechanismEntity();
            mechanismEntity.setUserNubmer(mbeList.get(x).getMerchantBasicsUserNo())
                    .setName(mbeList.get(x).getMerchantBasicsName())
                    .setProvince(mbeList.get(x).getMerchantBasicsProvince())
                    .setCity(mbeList.get(x).getMerchantBasicsCity())
                    .setAddress(mbeList.get(x).getMerchantBasicsAddress())
                    .setPosition(mbeList.get(x).getMerchantBasicsPosition().toString())
                    .setContactsName(mbeList.get(x).getMerchantBasicsContactsName());
            mechanismMapper.insert(mechanismEntity);
        }
        apiResult.setSuccess(true);
        return apiResult;
    }
}